#ifndef ESP32_H
#define ESP32_H
#include <at32f4xx.h>
#include "crc.h"

#define RX_MAX_FRAME_LEN 50
#define TX_MAX_FRAME_LEN 50



typedef struct __attribute__ ((packed))
{
	volatile uint8_t data_length;		//接收到的数据长度
    volatile uint8_t frame_complete;	// 帧接收完成标志
} frame_receiver_t;
typedef struct 
{
	uint16_t page_addr;
	uint16_t variable_addr;
	uint16_t variable_value;
	uint16_t variable_flag;
	uint8_t  change_page_flag;				//如果切换页面，则优先切换页面，如果不切换
	uint8_t  change_valuable_flag;			//页面则一直处理数据。
}current_t;

typedef enum
{
	OTHERS,
	RIGHT_ENCODER_TURN = 0x9991,
	LEFT_ENCODER_TURN,
	RIGHT_ENCODER_KEY,
	LEFT_ENCODER_KEY,
	RIGHT_KEY,
	LEFT_KEY,
}operate_type_t;

typedef struct 
{
	uint8_t variable_3760;
	uint8_t variable_3761;
	uint8_t variable_3762;
	uint8_t variable_3763;
	uint8_t variable_3764;
	uint8_t variable_3765;
}all_variable_t;
extern uint8_t tx_buffer[TX_MAX_FRAME_LEN];
extern frame_receiver_t receiver;
extern volatile uint8_t dma_rx[RX_MAX_FRAME_LEN];
extern uint8_t tx_buffer[TX_MAX_FRAME_LEN];
extern current_t current;


void usart_init(void);
void esp32_init(void);
void process_frame(void);
void usart2_sendstr(uint8_t *str, uint16_t len);

void set_page_buff(uint16_t page_value,uint8_t* tx_buffer);
void set_variable_buff(uint16_t variable_addr, uint8_t* tx_buffer,uint8_t variable_value);
void process_tx_sequence(current_t* current);

void set_tx_buffer(uint16_t page_value, uint16_t variable_addr,uint8_t* tx_buffer,uint8_t variable_value);
#endif

